package com.xinhua.erp.service;

import com.xinhua.erp.pojo.PageResult;
import com.xinhua.erp.pojo.Result;
import com.xinhua.erp.pojo.Supplier;
import com.xinhua.erp.repo.SupplierRepo;
import com.xinhua.erp.utils.IdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SupplierService {

    @Autowired
    private SupplierRepo supplierRepo;

    @Autowired
    private IdWorker idWorker;


    public Result findById(long id){

        return Result.OK(supplierRepo.getOne(id));
    }

   /* public Result findAll(int page,int size){

        Pageable pageable = PageRequest.of(page - 1, size);

        Page<Supplier> suppliers = supplierRepo.findAll(pageable);

        return Result.OK(new PageResult<>(suppliers.getTotalElements(),suppliers.getContent()));
    }*/

    public Result findAllByType(String type) {

        List<Supplier> list = supplierRepo.findAllByType(type);

        return Result.OK(list);

    }

    public Result findSupplierByType(String type,int page,int size){

        Pageable pageable = PageRequest.of(page-1, size);

        Page<Supplier> suppliers = supplierRepo.findAllByType(type, pageable);

        PageResult<Supplier> pageResult = new PageResult<>(suppliers.getTotalElements(), suppliers.getContent());

        return Result.OK(pageResult);
    }

    public Result deleteById(long id){

        supplierRepo.deleteById(id);

        return Result.OK();
    }

    public Result save(Supplier supplier){

        supplier.setId(idWorker.nextId());
        supplier.setAllneedget(new BigDecimal(0));
        supplier.setAllneedpay(new BigDecimal(0));
        //设置默认值

        supplierRepo.save(supplier);

        return Result.OK();
    }

    public Result update(Long id,Supplier supplier){
        supplier.setId(id);
        supplierRepo.save(supplier);
        return Result.OK();
    }

    public Result updateAllneedget(Long id, BigDecimal bigDecimal){
        Supplier one = supplierRepo.getOne(id);
        one.setAllneedget(one.getAllneedget().add(bigDecimal));
        supplierRepo.save(one);
        return Result.OK();
    }
    public Result updateAllneedpay(Long id,BigDecimal bigDecimal){
        Supplier one = supplierRepo.getOne(id);
        one.setAllneedpay(one.getAllneedpay().add(bigDecimal));
        supplierRepo.save(one);
        return Result.OK();
    }


    public Result Search(Map searchMap,int page,int size) {

        System.out.println(searchMap.toString());

        Pageable pageable = PageRequest.of(page - 1, size);

        Page<Supplier> suppliers = supplierRepo.findAll(createSpecification(searchMap), pageable);

        return Result.OK(new PageResult<>(suppliers.getTotalElements(), suppliers.getContent()));
    }

    private Specification<Supplier> createSpecification(Map searchMap){
        return new Specification<Supplier>() {
            @Override
            public Predicate toPredicate(Root<Supplier> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                ArrayList<Predicate> predicateList = new ArrayList<>();

                if(!StringUtils.isBlank((String)searchMap.get("type"))){
                    predicateList.add(criteriaBuilder.like(root.get("type").as(String.class),"%"+searchMap.get("type")+"%"));
                }

                if(!StringUtils.isBlank((String)searchMap.get("supplier"))){
                    predicateList.add(criteriaBuilder.like(root.get("supplier").as(String.class),"%"+searchMap.get("supplier")+"%"));
                }

                if(!StringUtils.isBlank((String)searchMap.get("telephone"))){
                    predicateList.add(criteriaBuilder.like(root.get("telephone").as(String.class),"%"+searchMap.get("telephone")+"%"));
                }

                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }
}

